﻿/*******
make sure you implement SyncSprite_imp
usage:

import com.cf.core.control.SyncSprite; // include SyncSprite.as
public class XXX extends YourClass Implement SyncSprite_imp{}


//*****/
package com.cf.core.control{
	import flash.display.Sprite;
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	
	public class SyncSprite extends Sprite{
		private var G_objA:DisplayObjectContainer;	
		private var G_event:Event;
		private var G_width,G_height,G_x,G_y,G_scaleX,G_scaleY,G_alpha,G_depth,G_cover:Boolean;
		
		public function SyncSprite():void{
			G_width=false;
			G_height=false;
			G_x=false;
			G_y=false;
			G_scaleX=false;
			G_scaleY=false;
			G_alpha=false;
			G_depth=false;
			G_event=new Event(Event.RESIZE);
		}//fn
		
		public function syncFrom(_oa:DisplayObjectContainer):void{
			G_objA=_oa;
			G_objA.addEventListener(Event.RESIZE, handleSync);	// target DisplayObject must contain SyncSprite
		}//fn
		
		private function handleSync(e:Event):void{
			if(this.syncWidth)		this.width=e.target.width;
			if(this.syncHeight)		this.height=e.target.height;
			if(this.syncX)			this.x=e.target.x;
			if(this.syncY)			this.y=e.target.y;
			if(this.syncAlpha)		this.alpha=e.target.alpha;
			if(this.syncScaleX)		this.scaleX=e.target.scaleX;
			if(this.syncScaleY)		this.scaleY=e.target.scaleY;
			if(G_depth){
				if(G_cover)			e.target.parent.setChildIndex(this, e.target.getChildIndex+1);
				else				e.target.parent.setChildIndex(this, e.target.getChildIndex-1);
			}
		}//fn
		
		public function get target():DisplayObjectContainer{return G_objA}
		public override function set height(_n:Number):void{super.height=_n;	dispatchEvent(G_event);}
		public override function set width(_n:Number):void{super.width=_n;		dispatchEvent(G_event);}
		public override function set x(_n:Number):void{super.x=_n;				dispatchEvent(G_event);}
		public override function set y(_n:Number):void{super.y=_n;				dispatchEvent(G_event);}
		public override function set alpha(_n:Number):void{super.alpha=_n;		dispatchEvent(G_event);}
		public override function set scaleX(_n:Number):void{super.scaleX=_n;	dispatchEvent(G_event);}
		public override function set scaleY(_n:Number):void{super.scaleY=_n;	dispatchEvent(G_event);}
		public override function setChildIndex(_c:DisplayObject, _n:int):void{
			super.setChildIndex(_c,_n);
			dispatchEvent(G_event);
		}//fn
		
		public function set syncWidth(_tf:Boolean):void{G_width=_tf}
		public function get syncWidth():Boolean{return G_width}
		public function set syncHeight(_tf:Boolean):void{G_height=_tf}
		public function get syncHeight():Boolean{return G_height}
		public function set syncX(_tf:Boolean):void{G_x=_tf}
		public function get syncX():Boolean{return G_x}
		public function set syncY(_tf:Boolean):void{G_y=_tf}
		public function get syncY():Boolean{return G_y}
		public function set syncAlpha(_tf:Boolean):void{G_alpha=_tf}
		public function get syncAlpha():Boolean{return G_alpha}
		public function set syncScaleX(_tf:Boolean):void{G_scaleX=_tf}
		public function get syncScaleX():Boolean{return G_scaleX}
		public function set syncScaleY(_tf:Boolean):void{G_scaleY=_tf}
		public function get syncScaleY():Boolean{return G_scaleY}
		
		//
		// Depth
		//
		public function set syncDepth(_tf:Boolean):void{
			this.syncDepthCover=_tf;
		}
		public function set syncDepthCover(_tf:Boolean):void{
			G_depth=_tf;
			G_cover=true;
		}
		public function set syncDepthRear(_tf:Boolean):void{
			G_depth=_tf;
			G_cover=false;
		}
		
	}//class
}//package